<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>海南空管局项目管理平台</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="<%=request.getContextPath()%>/huazhi/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="<%=request.getContextPath()%>/huazhi/css/public.css" media="all">
    <link rel="shortcut icon" href="<%=request.getContextPath()%>/huazhi/images/favicon.ico"/>
    <link rel="stylesheet" href="<%=request.getContextPath()%>/huazhi/lib/process/demo.css" media="all">
    <link rel="stylesheet" href="<%=request.getContextPath()%>/huazhi/lib/process/chart.css">
</head>
<style>
    /* 底部固定区域 */
    .layui-layout-admin .layui-footer {
        padding: 10px 0;
        text-align: center;
    }

    .layui-layout-admin .layui-footer {
        background-color: #fff;
    }
</style>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-row layui-col-space15">
                <div class="layui-tab-content">
                    <div class="layui-form">
                        <div class="layui-col-xs6">
                            <div class="layui-form-item">
                                <div class="layui-col-xs11">
                                    <label class="layui-form-label">流程名</label>
                                    <div class="layui-input-block">
                                        <input type="text" name="processName" lay-verify="processName"
                                               id="processName"
                                               autocomplete="off" placeholder="请输入流程名称"
                                               class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-col-xs1">
                                    <div class="layui-form-mid layui-word-aux"
                                         style="color: red !important;padding: 10px !important;">*
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-col-xs11">
                                    <label class="layui-form-label">流程类别</label>
                                    <div class="layui-input-block">
                                        <select id="types" disabled>
                                            <option value="">请选流程类别</option>
                                            <option value="one">采购流程</option>
                                            <option value="two">领用流程</option>
                                            <option value="three">出库流程</option>
                                            <option value="four">入库流程</option>
                                        </select>
                                    </div>
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-col-xs11">
                                    <label class="layui-form-label">描述</label>
                                    <div class="layui-input-block">
                                            <textarea class="layui-textarea" name="processRemarks" id="processRemarks"
                                                      placeholder="请输入描述"></textarea>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-md12">
                            <div class="layui-form">
                                <div class="layui-form-item">
                                    <div class="layui-col-xs6">
                                        <div class="layui-col-xs6">
                                            <div class="layui-inline" style="margin-bottom: 0px;margin-right: 0px;width: 83%">
                                                <label class="layui-form-label">部门</label>
                                                <div class="layui-input-block">
                                                    <select id="departmenttid">
                                                        <option value="">请选择</option>
                                                        <option value="申请人部门">申请人部门</option>
                                                    </select>
                                                </div>
                                            </div>
                                        </div>
                                        <div class="layui-col-xs6">
                                            <div class="layui-inline" style="margin-bottom: 0px;margin-right: 0px;width: 83%">
                                                <label class="layui-form-label">职位</label>
                                                <div class="layui-input-block">
                                                    <select id="positiontid">
                                                        <option value="">请选择</option>
                                                    </select>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="layui-inline" style="margin-bottom: 0px;margin-right: 0px;">
                                        <div class="layui-input-inline">
                                            <button class="layui-btn" id="change" data-type="reload">添加流程步骤</button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-md12">
                        <div id="demo-chart">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item layui-layout-admin">
                    <div class="layui-footer" style="left: 0;padding: 0px;">
                        <button class="layui-btn" id="pushButton" data-type="reload">确定修改流程</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<input type="hidden" id="jsonOutput">
<script src="<%=request.getContextPath()%>/huazhi/lib/layui-v2.5.5/layui.all.js"></script>
<script src="<%=request.getContextPath()%>/huazhi/lib/process/jsPlumb.min.js"></script>
<script src="<%=request.getContextPath()%>/huazhi/lib/process/chart.js"></script>
<script src="<%=request.getContextPath()%>/huazhi/lib/jquery-3.4.1/jquery-3.4.1.min.js"></script>
<script>
    function child(tid) {
        layui.use(['table', 'form'], function () {
            var $ = layui.$
                , table = layui.table
                , layer = layui.layer,
                dtree = layui.dtree,
                form = layui.form;

            $.ajax({
                type: "post",
                url: "<%=request.getContextPath()%>/rest/system/getDepartmentLists",
                dataType: "json",
                async: false,
                success: function (msg) {
                    var select = document.getElementById("departmenttid");
                    $.each(msg.data, function (i, n) {
                        select.options.add(new Option(n.departmentname, n.departmentname));
                    })
                    form.render('select');
                    return false;
                }
            });

            $.ajax({
                type: "post",
                url: "<%=request.getContextPath()%>/rest/system/getListPosition",
                dataType: "json",
                async: false,
                success: function (msg) {
                    var select = document.getElementById("positiontid");
                    $.each(msg.data, function (i, n) {
                        select.options.add(new Option(n.positionname, n.positionname));
                    })
                    form.render('select');
                    return false;
                }
            });



            var newX = 60;
            var newY = 60;

            var _createChart = function () {
                return new Chart($('#demo-chart'), {})
            };

            $.ajax({
                type: "post",
                url: "<%=request.getContextPath()%>/rest/process/getProcessDetailForUpdate",
                dataType: "json",
                data: "tid=" + tid,
                async: false,
                success: function (data) {
                    $("#processName").val(data.title)
                    $("#processRemarks").val(data.remarks)
                    chart = _createChart();
                    chart.fromJson(JSON.stringify(data.forShow));
                    $("#types").val(data.types)
                    form.render('select');
                }
            })

            var addNewTask = function (name, params) {
                params = params || {};
                params.data = params.data || {};
                params.class = 'node-process';
                params.data.nodeType = 1; // 流程节点类型
                var node = chart.addNode(name, newX, newY, params);
                node.addPort({
                    isSource: true
                });
                node.addPort({
                    isTarget: true,
                    position: 'Top'
                });
            };

            $("#change").on('click', function (event) {
                var departmenttid = $("#departmenttid").val()
                var positiontid = $("#positiontid").val()

                if ('' == departmenttid) {
                    var departmenttidcheck = document.getElementById("departmenttid");
                    departmenttidcheck.setAttribute("class", "layui-form-danger");
                    departmenttidcheck.focus();
                    layer.msg("请选择部门", {icon: 5, shift: 0});
                    return
                }

                if ('' == positiontid) {
                    var positiontidcheck = document.getElementById("positiontid");
                    positiontidcheck.setAttribute("class", "layui-form-danger");
                    positiontidcheck.focus();
                    layer.msg("请选择职位", {icon: 5, shift: 0});
                    return
                }

                $(this).data('node', $.extend({}, {
                    name: departmenttid + '-' + positiontid,
                    procId: '0',
                    // username: username,
                    desc: departmenttid + '-' + positiontid
                }));

                var target = $(event.target);
                var node = target.data('node');
                addNewTask(node.name, {
                    data: node
                });
            });

            $('#pushButton').on('click', function () { //搜索的id
                $('#jsonOutput').val(JSON.stringify(chart.toJson()));
                var detail = $("#jsonOutput").val()

                var processName = $("#processName").val();
                var processRemarks = $("#processRemarks").val();

                if (processName == "") {
                    layer.msg("流程名必填", {icon: 5, shift: 0});
                    return false;
                }

                var index = layer.msg('数据提交中，请稍候', {icon: 16, time: false, shade: 0.6});
                $("#pushButton").addClass("layui-btn layui-btn-disabled").prop("disabled", true);
                $.ajax({
                    type: "post",
                    url: "<%=request.getContextPath()%>/rest/process/updateProcessAll",
                    dataType: "json",
                    data: "processPackage=" + detail + "&processName=" + processName + "&processRemarks=" + processRemarks + "&oldProcessTid=" + tid,
                    async: false,
                    success: function (msg) {
                        setTimeout(function () {
                            layer.close(index);
                            if (msg == true) {
                                layer.msg('修改成功');
                            } else {
                                layer.msg(msg.errorMsg);
                            }

                        }, 2000);
                        setTimeout(function () {
                            var indexs = parent.layer.getFrameIndex(window.name);//获取当前弹出层的层级
                            parent.layer.close(indexs);//关闭弹出层
                        }, 4000);
                    }
                })
            });

        });
    }
</script>
</body>
</html>
